package com.plasticene.boot.banner.core;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.plasticene.boot.common.constant.OrderConstant;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.annotation.Order;

import java.util.concurrent.TimeUnit;

/**
 * @author fjzheng
 * @version 1.0
 * @date 2022/7/12 15:11
 */
@Slf4j
@Order(OrderConstant.RUNNER_BANNER)
public class BannerApplicationRunner implements ApplicationRunner {

    @Resource
    private ServerProperties serverProperties;

    @Value("${spring.application.name}")
    private String name;

    private static final String URL_PREFIX = "http://127.0.0.1:";
    private static final String SWAGGER_SUFFIX = "/doc.html";

    @Override
    public void run(ApplicationArguments args) {
        StringBuilder builder = new StringBuilder(URL_PREFIX + serverProperties.getPort());
        String contextPath = serverProperties.getServlet().getContextPath();
        if (StrUtil.isNotBlank(contextPath)) {
            if (! contextPath.startsWith("/")) {
                builder.append("/");
            }
            builder.append(contextPath);
        }
        builder.append(SWAGGER_SUFFIX);
        ThreadUtil.execute(() -> {
            // 延迟 1 秒，尽量保证输出到结尾
            ThreadUtil.sleep(1, TimeUnit.SECONDS);
            String template = """
                    \n----------------------------------------------------------
                    (♥◠‿◠)ﾉﾞ  {}启动成功啦   ლ(´ڡ`ლ)
                    接口文档:  {}
                    """;
            log.info(template, name, builder);
        });
    }

}
